import 'package:badges/badges.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:rc_app/contants/constant.dart';
import 'package:rc_app/router.dart';
import 'package:rc_app/utils/screen_utils.dart';
import 'package:rc_app/widgets/simple_image_button.dart';

//日常首页
class WorkPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _WorkPageState();
  }
}

class _Item {
  int index;
  String title;
  String icon;
  String badge;
  bool showBadge = false;

  _Item(this.index, this.title, this.icon,
      {this.badge = '', this.showBadge = false});
}

class _WorkPageState extends State<WorkPage> {
  var itemW;
  String _localUserName = '', _localUserOrg = '', _localUserIcon = '';
  int _unReadMsgCount = 0;
  bool showBadge = true;
  int bodyHeight = 0;
  Widget alarmWidget;
  List<_Item> alarmConfig = [
    _Item(0, '待接收', 'alarm_djs.png'),
    _Item(1, '待反馈', 'alarm_dfk.png'),
    _Item(2, '已反馈', 'alarm_yfk.png'),
  ];
  Widget workWidget;
  List<_Item> workConfig = [
    _Item(0, '现场接警', 'work_xcjj.png'),
    _Item(1, '重大警情', 'work_zdjq.png'),
    _Item(2, '警情查询', 'work_jqcx.png'),
    _Item(3, '周边警力', 'work_zbjl.png'),
    _Item(4, '信息查询', 'work_xxcx.png'),
    _Item(5, '勤务查询', 'work_qwcx.png'),
  ];

  //初始化用户信息
  void initUser() {
    Constant.getLocalUser().then((Map<String, String> localMap) {
      print('localMap' + localMap.toString());
      setState(() {
        _localUserName = localMap['userName'];
        _localUserOrg = localMap['userOrg'];
        _localUserIcon = localMap['userIcon'];
      });
    });
  }


  void _itemClick(_Item item) {
    print('menu click' + item.title + "" + (item.showBadge.toString()));
    Router.push(context, Router.alarmHandle, {'pageType': item.index});
  }

  @override
  void initState() {
    initUser();
    super.initState();
  }

  //头
  Widget _getHeader() {
    return Container(
      padding: EdgeInsets.only(top: ScreenUtils.screenH(context) * 0.25 * 0.4),
      child: Column(
        children: <Widget>[
          Image.asset(
            Constant.ASSETS_IMG + "work/account.png",
            fit: BoxFit.cover,
            width: 50,
            height: 50,
          ),
          Padding(
            padding: EdgeInsets.symmetric(vertical: 8),
            child: Text(
              '$_localUserName',
              style: TextStyle(fontSize: 13, color: Colors.white),
            ),
          ),
          Text(
            '($_localUserOrg)',
            style: TextStyle(fontSize: 13, color: Colors.white),
          ),
        ],
      ),
      decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage(Constant.ASSETS_IMG + "work/banner_bg.png"),
              fit: BoxFit.cover)),
    );
  }

  //日常警情
  Row getAlarmWidget() {
    var alarmListWidget = alarmConfig.map((item) {
      return SimpleImageButton(
        showBadge: item.showBadge,
        badge: item.badge,
        width: 35,
        padding: 10,
        title: item.title + '',
        icon: Constant.ASSETS_IMG + "work/" + item.icon,
        onPressed: () {
          _itemClick(item);
        },
      );
    }).toList();
    return Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: alarmListWidget);
  }

  //主菜单
  Widget _getWorkItem(_Item item) {
    return SimpleImageButton(
      color: Colors.white,
      icon: Constant.ASSETS_IMG + "work/" + item.icon,
      title: item.title,
      style: TextStyle(fontSize: 15),
      width: 45,
      padding: 8,
      onPressed: () {
        Fluttertoast.showToast(msg: item.title);
      },
    );
  }

  //主菜单Grid
  SliverGrid getWorkGrid() {
    return SliverGrid(
      delegate: SliverChildBuilderDelegate(
        (BuildContext context, int index) {
          return _getWorkItem(workConfig[index]);
        },
        childCount: workConfig.length,
      ),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 3,
          crossAxisSpacing: 0,
          mainAxisSpacing: 0.0,
          childAspectRatio: 1),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
      child: CustomScrollView(
        physics: const BouncingScrollPhysics(),
        slivers: <Widget>[
          SliverAppBar(
            floating: true,
            pinned: true,
            flexibleSpace: FlexibleSpaceBar(
              background: _getHeader(),
            ),
            expandedHeight: ScreenUtils.screenH(context) * 0.25,
            actions: <Widget>[
              Padding(
                padding: EdgeInsets.only(top: 20, right: 20),
                child: Badge(
                  showBadge: showBadge,
                  badgeContent: Text('$_unReadMsgCount'),
                  child: Icon(
                    Icons.notifications_none,
                    size: 30,
                  ),
                ),
              )
            ],
          ),
          SliverToBoxAdapter(
            child: Container(
              color: Colors.white,
              margin: EdgeInsets.symmetric(vertical: 8),
              padding: EdgeInsets.symmetric(vertical: 8),
              child: Container(
                child: getAlarmWidget(),
              ),
            ),
          ),
          SliverToBoxAdapter(
            child: Container(
              margin: EdgeInsets.only(top: 5),
              color: Colors.white,
              child: Column(
                children: <Widget>[
                  Container(
                    margin: EdgeInsets.only(top: 15),
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Padding(
                        padding: EdgeInsets.only(left: 20),
                        child: Text(
                          '日常办公',
                          style: TextStyle(fontSize: 16),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          getWorkGrid()
        ],
      ),
      color: Color(0xf1f1f1),
    ));
  }
}
